package action.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import model.HeadImg;
import model.User;
import model.UserFile;

import org.apache.struts2.ServletActionContext;

import DAO.UserDAO;
import DAO.DAOImple.UserDAOImple;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	private UserFile file;
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	private int ableSize;  //允许的文件大小
	private int size; //文件实际大小

	private User user;

	@Override
	public String execute() throws Exception {
		boolean result= uploadFile();
		return SUCCESS;
	}

	// 头像上传
	public String ModifyImg() {
		ActionContext act = ActionContext.getContext();
		UserDAO userdao = new UserDAOImple();
		String stringResult = "imgerror";
		setAbleSize(1024*200); //设置图片大小200k
		String fileType = null;
		if(getUploadFileName() != null) {  //判断文件是否为空
			fileType = getUploadFileName().substring((getUploadFileName().lastIndexOf(".") + 1));
			Pattern p = Pattern.compile("jpg|gif|png");  //判断文件格式
			Matcher m = p.matcher(fileType);
			if(!m.matches()) {
				act.put("wrong", "0");//格式不正确
			}else {
				boolean result = uploadFile();  //文件上传，返回true 上传成功   false 上传失败
				//boolean result = false;
				if(result) {
					HeadImg headImg = new HeadImg();
					headImg.setHeadName(getUploadFileName());
					headImg.setImgSrc("userfile");
					
					int crs = userdao.changeHeadImg(user, headImg);
					if(crs == 0) {
						act.put("wrong", "2");//更改头像失败
						stringResult = "imgerror";
					}else {
						stringResult = "imgsuccess";  //更改头像成功
					}
				}else {
					act.put("wrong", "3");//文件不能大于1M
					act.put("filesize", getSize());
					stringResult = "imgerror";
				}
			}
		}else {
			act.put("wrong", "1");//文件为空
			stringResult = "imgerror";
		}
		
		user = userdao.getUser(user.getId());
		return stringResult;
	}

	// 文件上传
	public boolean uploadFile() {
		boolean result = false;  //默认为false上传不成功
		if(getUploadFileName() != null) {
			String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())
							+ (int)(Math.random()*100000);
			String fileType = getUploadFileName().substring((getUploadFileName().lastIndexOf(".")));
			setUploadFileName(fileName + fileType);
			try {
				FileInputStream fis = new FileInputStream(getUpload());
                setSize(fis.available());   // 所上传文件的真实大小
	            if(getSize() <= getAbleSize()){   //文件小于1M
	            	FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
	            	byte[] buffer = new byte[1024];
					int len = 0;
					
					while ((len = fis.read(buffer)) > 0) {
						fos.write(buffer, 0, len);
					}
					
					result = true;
	            }
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else {
			//文件为空
		}
		return result;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		String realPath = ServletActionContext.getRequest().getRealPath(
				savePath);
		return realPath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public UserFile getFile() {
		return file;
	}

	public void setFile(UserFile file) {
		this.file = file;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getAbleSize() {
		return ableSize;
	}

	public void setAbleSize(int ableSize) {
		this.ableSize = ableSize;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
}
